// Dynare code ARGEMminJournalArticle.mod 
// to be used with Dynare version 4.2.0

// "A DSGE model for a SOE with Systematic Interest and Foreign Exchange policies 
// in which policymakers exploit the risk premium for stabilization purposes"
// Guillermo J. Escud�. July 2013
// Economics e-Journal

// 3 general regimes:
// MER: Managed Exchange Rate regime:  2 rules
// FER: Floating Exchange Rate regime: 1 rule for the nominal interest rate
// PER Pegged Exchange Rate regime:    1 rule for the rate of nominal depreciation

// For simple rules or optimal simple rules ('osr'):
// For the MER regime: Use the 2 policy rules
// For the FER regime: Replace the second policy rule by equation 'r=r_ss' 
// For the PER regime: Replace the first policy rule by equation 'b=b_ss' 

// For 'ramsey_policy':
// Use 'planner_objective' 
// Comment out 'steady', 'check' and 'resid' 
// For MER regime use option 'instruments=(ii,delta)' and comment out the 2 policy rules 
// For FER regime use option 'instruments=(ii)' and the equation 'r=r_ss' 
// For PER regime use option 'instruments=(delta)' and the equation 'b=b_ss'


close all;

// ENDOGENOUS VARIABLES

var ii delta r b m e piC Y d  C pii Q mc Deltta pC pStar X TB CA 
GammaP PsiP w N tax qf gammaD gammaR gammaM varphiD tauD varphiM tauM 
z_G z_epsilon z_iStar z_phiStar z_piStar z_piStarX 
real_ii CBbalsheet Utility dii ddelta ;

// EXOGENOUS VARIABLES

varexo eps_epsilon eps_G eps_iStar eps_phiStar eps_piStar eps_piStarX ; 


// PARAMETERS

parameters 

// Simple policy rules
h_0 h_1 h_2 h_3               //Interest rate  
k_0 k_1 k_2 k_3 k_4           //Rate of nominal depreciation 

// Persistence parameters in shock variables          
rhoG rhoEpsilon rhoiStar rhoPhiStar rhoPiStar rhoPiStarX rhoPiStarNX

// Great ratios  
gammaR_ss gammaD_ss gammaM_ss gammaG pCCtoy mtoy btoy tbetoy 

// General                                                                           
piT betta sigmaC a_D thetta thettaC sigmaN alfa bA bX kappaX xiN 
varepsilon_L  alpha_1 alpha_2 alpha_3 betta_1 betta_2 betta_3
EC_speed_N EC_speed_X  
ELASTvarphiD_1 ELASTvarphiD_2 ELASTtauBarD_1 ELASTtauBarD_2

// Steady state values of endogenous variables
Y_ss piStar_ss iStar_ss phiStar_ss pStar_ss  e_ss C_ss d_ss r_ss
mc_ss Q_ss GammaP_ss PsiP_ss N_ss pC_ss m_ss b_ss X_ss TB_ss 
Deltta_ss w_ss varphiM_ss tauM_ss tax_ss qf_ss Utility_ss 
tauD_ss varphiD_ss 

//Weights for ramsey ad-hoc CB loss function
omega_piC omega_Y omega_e omega_r omega_dii omega_ddelta ;

// PARAMETER VALUES

// Simple policy rules
h_0=0.33; h_1=1.26; h_2=0.02;   h_3=0.12; 
k_0=-0.03; k_1=-0.07; k_2=-0.08; k_3=-0.43; k_4=-0.08;      

// Calibrated NSS values of endogenous variables
Y_ss=1.443; piStar_ss=1.0; iStar_ss=1.03^0.25; pStar_ss=1; phiStar_ss=1.005^0.25;

// Persistence parameters in shock variables 
rhoEpsilon=0.8; rhoG=0.85; rhoiStar=0.7; rhoPhiStar=0.3;
rhoPiStar=0.20; rhoPiStarX= 0.41 ; rhoPiStarNX=0.18;

betta=0.99;            //Intertemporal discount factor                    
piT=1.015;             //Inflation target       
alfa=0.66;             //Probability of non-optimizing price
sigmaC=1.5;            //Constant relative risk aversion for consumption
sigmaN=0.5 ;           //Constant relative risk aversion for labor 
a_D=0.86;              //Home bias in consumption
thetta=6;              //Elasticity of substitution for domestic goods
thettaC=1.5;           //Elasticity of substitution between domestic and imported bundles
bA=0.5;                //Commodities production function

bX=1/(1-bA);  kappaX=bA^(bA/(1-bA));  //Exports composite parameters
EC_speed_N=0.181; EC_speed_X=-0.255;  //Cointegration in trade prices parameters 

// Elasticities or risk premium:
ELASTtauBarD_1=10;     //Debt/GDP ratio elasticity of tauD 
ELASTtauBarD_2=0;      //Reserves/GDP ratio elasticity of tauD
    // These are only for information:
ELASTvarphiD_1=2*ELASTtauBarD_1*(1-betta*phiStar_ss*iStar_ss); 
ELASTvarphiD_2=(-ELASTtauBarD_2)*(1/(1+ELASTtauBarD_1)-2)*(1-betta*phiStar_ss*iStar_ss);


// Risk premium parameters and variables 
gammaD_ss=0.5;         //Household Foreign Debt to GDP ratio 
gammaR_ss=0.13;           //CB reserves to GDP ratio
alpha_1=(1/(betta*phiStar_ss*iStar_ss/piStar_ss)-1)/((1+ELASTtauBarD_1)*(1+ELASTtauBarD_1+ELASTtauBarD_2));
alpha_2=(ELASTtauBarD_1/(1+ELASTtauBarD_1+ELASTtauBarD_2))/gammaD_ss;
alpha_3=((-ELASTtauBarD_2)/(1+ELASTtauBarD_1+ELASTtauBarD_2))/gammaR_ss;
tauD_ss=1+alpha_1/(1-alpha_2*gammaD_ss+alpha_3*gammaR_ss);                            
varphiD_ss=1+(tauD_ss-1)*(1+alpha_3*gammaR_ss)/(1-alpha_2*gammaD_ss+alpha_3*gammaR_ss);

// RER determination
tbetoy=((iStar_ss/piStar_ss)*phiStar_ss*tauD_ss-1)*gammaD_ss-(iStar_ss/piStar_ss-1)*gammaR_ss;
f_e = @(e_ss) (kappaX*(e_ss*pStar_ss)^bX*(a_D+(1-a_D)*e_ss^(1-thettaC))-(1-a_D)*e_ss^(1-thettaC))/a_D-tbetoy;
e_ss=fzero(f_e,0.595);                       

// Transactions cost parameters and variables 
gammaM_ss=0.095522;         //money/consumption ratio
varepsilon_L=1.02 ;         //Elasticity of money/consumption function w.r. to ii
betta_3=160;                //Should be >1/(varepsilon_L*(piT/betta-1))-1 
betta_2=(1/gammaM_ss)*(1/(((betta_3+1)*varepsilon_L*(piT/betta-1))-1));                        
betta_1=(1-betta/piT)*((1+betta_2*gammaM_ss)^(betta_3+1))/(betta_2*betta_3);          
tauM_ss=1+betta_1/(1+betta_2*gammaM_ss)^betta_3;
varphiM_ss=1+(tauM_ss-1)*(1+betta_3*((betta_2*gammaM_ss)/(1+betta_2*gammaM_ss))); 

// Other great ratios
gammaG=0.19;                //Gov expenditure to private consumption ratio
pC_ss=(a_D+(1-a_D)*(e_ss)^(1-thettaC))^(1/(1-thettaC));    
pCCtoy=(1-kappaX*(e_ss*pStar_ss)^bX)*(pC_ss^(1-thettaC)/(a_D*(1+gammaG)*(tauM_ss)));
mtoy=gammaM_ss*pCCtoy;     //money/GDP ratio
btoy=gammaR_ss-mtoy;       //bonds/GDP ratio

// Steady state values of remaining endogenous variables
X_ss=Y_ss*kappaX*(e_ss*pStar_ss)^bX;
Q_ss=Y_ss-(1-bA)*X_ss;
Deltta_ss=((1-alfa)/((1-alfa*piT^(thetta))))*((1-alfa*piT^(thetta-1))/(1-alfa))^(thetta/(thetta-1));
N_ss=Q_ss*Deltta_ss;
C_ss=pCCtoy*Y_ss/pC_ss;         
GammaP_ss=((Q_ss/(pC_ss*C_ss^sigmaC))/(1-betta*alfa*piT^(thetta-1)));
PsiP_ss=GammaP_ss/(((1-alfa*piT^(thetta-1))/(1-alfa))^(1/(thetta-1)));
mc_ss=PsiP_ss*(1-betta*alfa*((piT)^thetta))/((thetta/(thetta-1))*(Q_ss/(pC_ss*C_ss^sigmaC)));    
w_ss=mc_ss;
r_ss=gammaR_ss*Y_ss/e_ss;
b_ss=btoy*Y_ss;
qf_ss=((iStar_ss)-1/(piT/piStar_ss))*(e_ss*r_ss/piStar_ss)-(piT/betta-1)*(b_ss/piT);
tax_ss=gammaG*tauM_ss*pC_ss*C_ss-qf_ss;
TB_ss=tbetoy*Y_ss/e_ss;
d_ss=gammaD_ss*Y_ss/e_ss;
m_ss=mtoy*Y_ss;
xiN=w_ss/(N_ss^sigmaN*pC_ss*C_ss^sigmaC*varphiM_ss);
Utility_ss=(1/(1-sigmaC))*C_ss^(1-sigmaC)-(xiN/(1+sigmaN))*N_ss^(1+sigmaN);

//For 'ramsey' objective:
omega_piC=100; omega_Y=1; omega_e=1; omega_r=1; omega_dii=50; omega_ddelta=50 ;


// MODEL EQUATIONS

model;

// Simple policy rules (comment out both for ramsey in MER regime)

// 1 Taylor rule

ii=(piT/betta)^(1-h_0)*(ii(-1))^h_0*(piC/piT)^h_1*(Y/Y_ss)^h_2*(e/e_ss)^h_3;
//For Pegged Exchange Rate (PER) policy use instead:
//b=b_ss;

// 2 Exchange rate depreciation rule:

delta=(piT/piStar_ss)^(1-k_0)*delta(-1)^k_0*(piC/piT)^k_1*(Y/Y_ss)^k_2*(e/e_ss)^k_3*(gammaR/gammaR_ss)^k_4;
//For Floating Exchange Rate (FER) policy use instead:
//r=r_ss;

// 3 Central Bank balance sheet

b=e*r-m;

// 4 Money market clearing

m=(1/betta_2)*(((betta_1*betta_2*betta_3/(1-1/ii))^(1/(betta_3+1)))-1)*pC*C;    

// 5 Trade Balance (in foreign currency)

TB=(1/(a_D*e))*(pC^(1-thettaC)*X-(1-a_D)*e^(1-thettaC)*Y);

// 6-7 Current account (in foreign currency)

tauD=1+alpha_1/(1-alpha_2*gammaD+alpha_3*gammaR);

CA=(exp(z_iStar(-1))/exp(z_piStar)-1)*r(-1)-((exp(z_iStar(-1))/exp(z_piStar))*exp(z_phiStar(-1))*tauD(-1)-1)*d(-1)+TB;

// 8 Balance of Payments  (in foreign currency)

r=d+CA+r(-1)-d(-1);

// 9 Exports 

X=Y*kappaX*(e*pStar)^bX;

// 10 RER identity

e=e(-1)*(delta*exp(z_piStar)/pii);

// 11-12  Risk-adjusted uncovered interest parity 

varphiD=1+(tauD-1)*(1+alpha_2*gammaD/(1-alpha_2*gammaD+alpha_3*gammaR));

ii=exp(z_iStar)*exp(z_phiStar)*varphiD*delta(+1); 

// 13 Labor market clearing

w=xiN*N^sigmaN*pC*C^sigmaC*varphiM;

// 14 Hours worked

N=Q*Deltta/exp(z_epsilon);

// 15 Domestic goods market clearing

Q=Y-(1-bA)*X;

// 16 GDP 

Y=a_D*exp(z_G)*tauM*pC^thettaC*C+X;         

// 17 Dynamics of Price dispersion

Deltta=alfa*pii^thetta*Deltta(-1)+(1-alfa)*((1-alfa*pii^(thetta-1))/(1-alfa))^(thetta/(thetta-1));

// 18-20 Domestic inflation Phillips equation 

GammaP=PsiP*((1-alfa*pii^(thetta-1))/(1-alfa))^(1/(thetta-1));

GammaP=Q/(pC*C^sigmaC)+betta*alfa*(pii(+1))^(thetta-1)*GammaP(+1);       

PsiP=(thetta/(thetta-1))*(Q/(pC*C^sigmaC))*mc+betta*alfa*((pii(+1))^thetta)*PsiP(+1);    

// 21 Real Marginal Cost

mc=w/exp(z_epsilon);

// 22 Transactions cost (gross)

tauM=1+betta_1/(1+betta_2*gammaM)^betta_3;

// 23 Transactions cost effect on the Euler equation (gross)

varphiM=1+(tauM-1)*(1+betta_3*((betta_2*gammaM)/(1+betta_2*gammaM))); 

// 24 Consumption relative price

pC^(1-thettaC)=a_D+(1-a_D)*e^(1-thettaC);   

// 25 Consumption inflation identity

piC=pii*pC/pC(-1);    

// 26 Euler equation

C^(-sigmaC)/varphiM=betta*ii*(C(+1)^(-sigmaC)/piC(+1))/varphiM(+1);

// 27 Quasi-fiscal surplus

qf=(exp(z_iStar(-1))-1/delta)*(e*r(-1)/exp(z_piStar))-(ii(-1)-1)*(b(-1)/pii);

// 28 Tax collection

tax=(exp(z_G)-1)*tauM*pC*C-qf;

// 29 External Terms of Trade

pStar=pStar(-1)*exp(z_piStarX)/(exp(z_piStar));     


// VARIABLES SUBJECT TO STOCHASTIC SHOCKS (they are in logs)

// 30-31 Cointegrated RW prices

z_piStarX=z_piStarX(-1)*rhoPiStarX+log(pStar(-1))*EC_speed_X+eps_piStarX;

z_piStar=z_piStar(-1)*rhoPiStar+log(pStar(-1))*EC_speed_N+z_piStar(-1)*rhoPiStarNX+eps_piStar;

// 32 Government expenditure shock 

z_G=(z_G(-1))*rhoG+log(1+gammaG)*(1-rhoG)+eps_G;

// 33 Transitory productivity shock 

z_epsilon=z_epsilon(-1)*rhoEpsilon+eps_epsilon;

// 34 RW riskfree interest rate shock 

z_iStar=(z_iStar(-1))*rhoiStar+log(iStar_ss)*(1-rhoiStar)+eps_iStar; 

// 35 RW Financing liquidity shock 

z_phiStar=z_phiStar(-1)*rhoPhiStar+log(phiStar_ss)*(1-rhoPhiStar)+eps_phiStar;//posiTIVE SHOCK!!

// 36-38 Great ratios

gammaD=d*e/Y;
gammaR=r*e/Y;
gammaM=m/(pC*C);

// 39 Real interest rate

real_ii=ii/piC(+1);

// 40 Period Utility 

Utility=(1/(1-sigmaC))*(C)^(1-sigmaC) - (xiN/(1+sigmaN))*(N)^(1+sigmaN);

// 41-42 Policy instruments differences for ramsey

dii=ii-ii(-1);

ddelta=delta-delta(-1);

// 43

CBbalsheet=m+b;

end;


//steady;
//resid;    
//check;


shocks;
var eps_epsilon;        stderr  0.01    ;   
var eps_G;              stderr  0.03    ;  
var eps_iStar;          stderr  0.0046  ;    
var eps_phiStar;        stderr  0.05    ; 
var eps_piStar;         stderr  0.0295  ;   
var eps_piStarX;        stderr  0.0424  ;   
end;


steady_state_model;
ii=piT/betta; delta=piT/piStar_ss; r=r_ss; b=b_ss; e=e_ss; piC=piT; Y=Y_ss; 
d=d_ss; GammaP=GammaP_ss; PsiP=PsiP_ss; C=C_ss; m=m_ss; N=N_ss; Deltta=Deltta_ss; 
pii=piT; Q=Q_ss;  w=w_ss;  mc=mc_ss; pC=pC_ss; varphiM=varphiM_ss;
tauM=tauM_ss; X=X_ss; TB=TB_ss; real_ii=ii/piC; tax=tax_ss; qf=qf_ss; 
CA=0; gammaD=gammaD_ss; gammaM=gammaM_ss; 
tauD=tauD_ss; varphiD=varphiD_ss; Utility=Utility_ss; gammaR=gammaR_ss;
pStar=pStar_ss; z_G=log(1+gammaG); z_epsilon=log(1); z_piStar=log(piStar_ss);  
z_iStar=log(iStar_ss); z_phiStar=log(phiStar_ss); z_piStarX=log(piStar_ss); 
CBbalsheet=m_ss+b_ss; dii=0; ddelta=0; 
end;


stoch_simul(order=1,irf=0) ;



// FOR OPTIMAL SIMPLE RULES: //////////////////////////////////////////////

//optim_weights;
//piC 100; Y 1; e 1;  dii 50; ddelta 50;  
//end;

//osr_params   h_0 h_1 h_2 h_3    k_0 k_1 k_2 k_3 k_4   ; 

//Provides numerical initialization for the optimization:
//h_0=1.5; h_1=0.5; h_2=0.2; h_3=0.0;                      				
//k_0=0.0; k_1=-0.2; k_2=-0.0; k_3=0.0; k_4=-0.05;				

//Instructs for osr
//osr(order=1,irf=0) ;
///////////////////////////////////////////////////////////////////////////



// FOR OPTIMAL POLICY WITH COMMITMENT: ////////////////////////////////////

//planner_objective(omega_piC*(piC-piT)^2+omega_Y*(Y-Y_ss)^2+omega_e*(e-e_ss)^2+omega_r*(r-r_ss)^2+omega_dii*dii^2+omega_ddelta*ddelta^2);

//For MER regime:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii,delta),irf=0);

//For FER regime:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii),irf=0) ; 

//For PER regime:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(delta),irf=0) ;
